import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, ModalController, ViewController, LoadingController } from 'ionic-angular';
import { LoginPage } from '../login/login';
import { Storage } from '@ionic/storage';
import { BaseUI } from '../common/baseui';
import { RestProvider } from '../../providers/rest/rest';
import { UserPage } from '../user/user';
import { ScanPage }  from '../scan/scan';
import { VersionPage } from '../version/version';

/**
 * Generated class for the MorePage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-more',
  templateUrl: 'more.html',
})
export class MorePage extends BaseUI{

  // 定义一个判断已经登录成功的变量
  public logined:boolean = false;
  // 定义一个判断没有登录成功的变量
  public notLogin:boolean = true;
  // 个人头像URL 
  public headface:string;
  userinfo: string[];

  constructor(public navCtrl: NavController
            , public navParams: NavParams
            , public modalCtrl: ModalController
            , public storage: Storage
            , public viewCtrl: ViewController
            , public rest: RestProvider
            , public loadCtrl: LoadingController) {
              super();
  }

  // 这是一个auglarer页面里面的一个生命周期的扣函数
  ionViewDidLoad() {
    this.loadUserPage();
  }

  loadUserPage() {
    this.storage.get('UserId').then((val) => {
      if (val != null) {
        //加载用户数据
        var loading = super.showLoading(this.loadCtrl, "加载中...");
        this.rest.getUserInfo(val)
          .subscribe(
            userinfo => {
              this.userinfo = userinfo;
              this.headface = userinfo["UserHeadface"] + "?" + (new Date()).valueOf();
              this.notLogin = false;
              this.logined = true;
              loading.dismiss();
            }
          );
      }
      else {
        this.notLogin = true;
        this.logined = false;
      }
    });
  }

  // 进入二维码扫描
  /**
   * 简单的页面跳转不太合适， 需要将页面间跳转的动画关掉，
   * 否则相机出不来
   *
   * @memberof MorePage
   */
  gotoScanQrcode() {
    this.navCtrl.push(ScanPage, null, {"animate": false});
  }


  showModal () {
    let _modal = this.modalCtrl.create(LoginPage);
    // 弹出式窗口后，需要添加一个回调的钩子函数， 用来处理， 当这个弹出的窗口被关闭时， 需要回调相应的方法
    _modal.onDidDismiss (()=>{
      console.log('>>>>>>>>>>>>>>>>>>>')
    })
    _modal.present();
  }

  gotoUserPage() {
    this.navCtrl.push(UserPage);
  }
  // 进入关于界面
  gotoVersions() {
    this.navCtrl.push(VersionPage);
  }
}
